<?php

namespace app\api\service;

use app\model\Activity as ActivityModel;
use app\model\Applet;
use app\model\Money;
use Carbon\Carbon;
use think\facade\Db;

class ActivityService
{
   public static function getActivity($params)
   {
       $now = date('Y-m-d H:i:s');
       $applet = Applet::where('app_id',$params['app_id'])->findOrFail();
       $activity = ActivityModel::where([
           'status' => ActivityModel::STATUS_VALID,
       ])
       ->whereRaw('json_contains(applet_ids,json_array(?))', [$applet->id])
           ->where('start_at','<=',$now)
           ->where('end_at','>',$now)
           ->field('id,name,cover_img,type,scene,recharge_price,send_currency,sum_currency,url,start_at,end_at')
           ->order('id','DESC')
           ->find();
       if ($activity){
           $activity['recharge_type'] = Money::TYPE_ACTIVITY;
           $activity['scene'] = collect($activity['scene'])->toArray();
           $activity['online_minutes'] = in_array(ActivityModel::SCENE_TEN_MIN,$activity->scene) ? 10 : 0;
           $desc = str_replace('{send_currency}',$activity->send_currency,config('activity.activity_desc.'.$activity->type));
           $desc = str_replace('{mid_autumn_icon}',config('activity.mid_autumn_icon'),$desc);
           $diffDay = (new Carbon())->diffInDays(Carbon::parse($activity->end_at));
           $activity['desc'] = $desc;
           $activity['remain_days'] = $diffDay >= 0 ? $diffDay + 1 : $diffDay = 0;
           $activity['diy_color'] = [
               //限时活动仅剩xx天颜色
               'remain_tips_color' => '#F9E9B8',
               //充值按钮背景颜色
               'pay_bg_color' => 'linear-gradient(153deg, #F59C27, #FACB62)'
           ];
       }
       return $activity;
   }
}